/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.upc.embajada.ws.jms;

import edu.upc.embajada.entity.Beca;
import edu.upc.embajada.utilitarios.EmbajadaException;
import edu.upc.embajada.utilitarios.Validador;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;

/**
 *
 * @author rv
 */
public class BecasQueueService {

    public void grabaCola(final int idPais, final int idCarrera, final int idTipoEstudio, final String email) throws EmbajadaException {

        if (Validador.esCorreo(email)) {

            ConnectionFactory conn = new ActiveMQConnectionFactory("tcp://localhost:61616");
            JmsTemplate jmsTemplate = new JmsTemplate(conn);

            jmsTemplate.setDefaultDestinationName("colaBecas");

            jmsTemplate.send(new MessageCreator() {
                @Override
                public Message createMessage(Session session) throws JMSException {

                    String msg = Integer.toString(idPais) + ";" + Integer.toString(idCarrera) + ";" + Integer.toString(idTipoEstudio) + ";" + email;
                    return session.createTextMessage(msg);
                }
            });
        }
        else {
            throw new EmbajadaException("El formato de correo es incorrecto");
        }

    }
}
